<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/12/15
 * Time: 14:08
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Modelname extends Base
{
    /**
     * 型号列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('model_name')
                ->count();// 获取总条数
            $lists = Db::name('model_name')
                ->field('mn_id, mn_name, mn_time, mn_update_time')
                ->page($Nowpage,$limits)
                ->order('mn_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加型号
     * @return mixed|\think\response\Json
     */
    public function modelAdd(){
        if(Request::instance()->isPost()){
            $mn_name = trim(input('mn_name'));
            $exist = Db::table('up_model_name')->where('mn_name', $mn_name)->find();
            if($exist) return json(['code' => 100, 'msg' => '型号已存在']);
            
            $res = Db::table('up_model_name')->insert([
                'mn_name' => ','.trim($mn_name,' |,').',',
                'mn_time' => date('Y-m-d H:i:s')
            ]);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }
    
    /**
     * 编辑
     * @return mixed|\think\response\Json
     */
    public function modelEdit(){
        $mn_id = input('mn_id');
        if(Request::instance()->isPost()){
            $mn_name = trim(input('mn_name'));
            $exist = Db::table('up_model_name')
                ->where(['mn_name' => $mn_name, 'mn_id' => ['<>', $mn_id]])
                ->find();
            if($exist) return json(['code' => 100, 'msg' => '型号已存在']);
            
            $res = Db::table('up_model_name')->update([
                'mn_id' => $mn_id,
                'mn_name' => ','.trim($mn_name,' |,').',',
                'mn_update_time' => date('Y-m-d H:i:s')
            ]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $_mn_name = Db::table('up_model_name')->where('mn_id', $mn_id)->value('mn_name');
        $_mn_name = trim($_mn_name,' |,');
        return $this->fetch('', ['mn_name' => $_mn_name, 'mn_id' => $mn_id]);
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function modelDel(){
        $mn_id = input('id');
        $res = Db::table('up_model_name')->delete($mn_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}